﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DeviceFinder.Models;

namespace DeviceFinder.DAL
{
    public class UploadFileDAL : BaseDAL
    {
        public IQueryable<UPLOADFILE> GetAllFileByDeviceID(int DeviceID)
        {
            var lstFile = from file in entities.UPLOADFILEs
                          where file.IDDEVICE == DeviceID
                          select file;
            return lstFile;
        }

        public UPLOADFILE GetFileByID(int ID)
        {
            UPLOADFILE result = (from file in entities.UPLOADFILEs
                                 where file.ID == ID
                                 select file).ToList<UPLOADFILE>()[0];
            return result;
        }

        public bool AddFileToDB(UPLOADFILE file)
        {
            entities.UPLOADFILEs.Add(file);
            if (entities.SaveChanges() > 0)
                return true;
            return false;
        }

        public bool DeleteFile(int ID)
        {
            var lstFile = from file in entities.UPLOADFILEs
                          where file.ID == ID
                          select file;

            if (lstFile != null && lstFile.Count() > 0)
            {
                try
                {
                    entities.UPLOADFILEs.Remove(lstFile.First());
                    if (entities.SaveChanges() > 0)
                        return true;
                }
                catch (System.Exception ex)
                {
                }
            }

            return false;
        }
    }
}